<template>
  <div claas="component-container">
    <!-- 搜索表单 -->
    <el-form ref="queryFormRef" :model="queryParams" :inline="true">
      <el-form-item>
        <el-button type="success" :icon="Plus" :disabled="!menuId" @click="handleAdd">新增</el-button>
        <el-button type="danger" :icon="Delete" :disabled="multiple" @click="handleDelete">删除</el-button>
      </el-form-item>
      <el-form-item prop="name">
        <el-input v-model="queryParams.name" placeholder="权限名称" clearable @keyup.enter="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :icon="Search" @click="handleQuery">搜索</el-button>
        <el-button :icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="permList"
      v-loading="loading"
      @selection-change="handleSelectionChange"
      border
      style="width: 100%"
    >
      <el-table-column type="selection" align="center" min-width="10%"/>
      <el-table-column label="权限名称" prop="name" align="center" min-width="25%"/>
      <el-table-column label="权限标识" prop="permission" align="center" min-width="35%"/>
      <el-table-column label="操作" align="center" min-width="30%">
        <template #default="scope">
          <el-button
            type="primary"
            :icon="Edit"
            circle
            plain
            @click="handleUpdate(scope.row)"
          />
          <el-button
            type="danger"
            :icon="Delete"
            circle
            plain
            @click="handleDelete(scope.row)"
          />
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页工具条 -->
    <pagination
      v-if="total > 0"
      :total="total"
      v-model:page="queryParams.current"
      v-model:limit="queryParams.pageSize"
      @pagination="handleQuery"
    />

    <!-- 表单弹窗 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="700px">
      <el-form
        ref="dataFormRef"
        :model="formData"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" placeholder="请输入权限名称"/>
        </el-form-item>

        <el-form-item label="权限标识" prop="urlPerm">
          <el-input placeholder="接口地址" v-model="urlPerm.requestPath">
            <template #prepend>
              <el-select placeholder="请求方式" v-model="urlPerm.requestMethod" style="width: 100px" clearable>
                <el-option v-for="item in requestMethodOptions" :key="item.value" :value="item.value" :label="item.label"/>
              </el-select>
              <el-select placeholder="所属服务" v-model="urlPerm.serviceName" style="width: 110px; margin-left: 20px" clearable>
                <el-option v-for="item in microServiceOptions" :key="item.value" :value="item.value" :label="item.label"/>
              </el-select>
              <el-select placeholder="请求端" v-model="urlPerm.requestClient" style="width: 100px; margin-left: 20px" clearable>
                <el-option v-for="item in requestClientOptions" :key="item.value" :value="item.value" :label="item.label"/>
              </el-select>
            </template>
          </el-input>
          <el-link v-show="urlPerm.requestMethod && urlPerm.serviceName && urlPerm.requestClient && urlPerm.requestPath">
            {{ urlPerm.requestMethod }}/{{ urlPerm.serviceName }}/{{ urlPerm.requestClient }}/{{ urlPerm.requestPath }}
          </el-link>
        </el-form-item>

      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
  import {getCurrentInstance, onMounted, reactive, ref, toRefs, watch,} from 'vue';

  import {addPerm, deletePerms, getPermFormDetail, listPermPages, updatePerm,} from '@/api/system/perm';

  import {ElForm, ElMessage, ElMessageBox} from 'element-plus';
  import {
    Search,
    Plus,
    Edit,
    Refresh,
    Delete,
  } from '@element-plus/icons-vue';
  import {Dialog, Option} from '@/types/common';

  import {PermFormData, PermItem, PermQueryParam,} from '@/types/api/system/perm';

  const {proxy}: any = getCurrentInstance();

  const queryFormRef = ref(ElForm);
  const dataFormRef = ref(ElForm);

  const props = defineProps({
    menuId: {
      type: String,
      default: () => {
        return '';
      },
    },
  });

  watch(
    () => props.menuId,
    (value) => {
      state.queryParams.menuId = value;
      handleQuery();
    }
  );

  const state = reactive({
    loading: true,
    // 选中ID数组
    ids: [],
    // 非单个禁用
    single: true,
    // 非多个禁用
    multiple: true,
    queryParams: {
      current: 1,
      pageSize: 10,
    } as PermQueryParam,
    permList: [] as PermItem[],
    total: 0,
    dialog: {
      visible: false,
    } as Dialog,
    formData: {} as PermFormData,
    rules: {
      name: [{required: true, message: '请输入权限名称', trigger: 'blur'}],
      permission: [{required: true, message: '请输入权限标识', trigger: 'blur'}],
    },
    microServiceOptions: [] as Option[],
    requestMethodOptions: [] as Option[],
    requestClientOptions: [] as Option[],
    urlPerm: {
      requestMethod: '',
      serviceName: '',
      requestClient: '',
      requestPath: '',
    },
  });

  const {
    loading,
    multiple,
    permList,
    total,
    dialog,
    formData,
    rules,
    microServiceOptions,
    requestMethodOptions,
    requestClientOptions,
    urlPerm,
    queryParams,
  } = toRefs(state);

  function handleQuery() {
    if (state.queryParams.menuId) {
      state.loading = true;
      listPermPages(state.queryParams).then(({data}) => {
        state.permList = data.list;
        state.total = data.total;
        state.loading = false;
      });
    } else {
      state.loading = false;
      state.permList = [];
      state.total = 0;
      state.queryParams.current = 1;
    }
  }

  function resetQuery() {
    queryFormRef.value.resetFields();
    handleQuery();
  }

  function handleSelectionChange(selection: any) {
    state.ids = selection.map((item: any) => item.id);
    state.single = selection.length !== 1;
    state.multiple = !selection.length;
  }

  /**
   * 加载字典数据
   */
  function loadDictOptions() {
    proxy.$getDictItemsByTypeCode('micro_service').then((response: any) => {
      state.microServiceOptions = response.data;
    });
    proxy.$getDictItemsByTypeCode('request_method').then((response: any) => {
      state.requestMethodOptions = response.data;
    });
    proxy.$getDictItemsByTypeCode('request_client').then((response: any) => {
      state.requestClientOptions = response.data;
    });
  }

  function handleAdd() {
    loadDictOptions();
    state.dialog = {
      title: '添加权限',
      visible: true,
    };
  }

  function handleUpdate(row: any) {
    loadDictOptions();
    state.dialog = {
      title: '修改权限',
      visible: true,
    };
    const id = row.id || state.ids;
    getPermFormDetail(id).then((response) => {
      const {data} = response;
      state.formData = data;
      if (data && data.permission) {
        const urlPermArr = data.permission.split('/');
        state.urlPerm.requestMethod = urlPermArr[0];
        state.urlPerm.serviceName = urlPermArr[1];
        state.urlPerm.requestClient = urlPermArr[2];
        // 这里分割几组,就需要在长度上加几.
        state.urlPerm.requestPath = data.permission.substring(state.urlPerm.requestMethod.length + state.urlPerm.serviceName.length + state.urlPerm.requestClient.length + 3);
      }
    });
  }

  function submitForm() {
    dataFormRef.value.validate((isValid: any) => {
      if (isValid) {
        // 接口权限和按钮权限必填其一
        if (!state.urlPerm.requestPath) {
          ElMessage.warning('请至少填写一种权限');
          return false;
        }
        // 如果填写了URL权限，完整性校验
        if (!state.urlPerm.requestMethod) {
          ElMessage.warning('URL权限的请求方式不能为空');
          return false;
        }
        if (!state.urlPerm.serviceName) {
          ElMessage.warning('URL权限的所属服务不能为空');
          return false;
        }
        if (!state.urlPerm.requestClient) {
          ElMessage.warning('URL权限的请求端不能为空');
          return false;
        }
        state.formData.permission = state.urlPerm.requestMethod + '/' + state.urlPerm.serviceName + '/' + state.urlPerm.requestClient + '/' + state.urlPerm.requestPath;
        if (state.formData.id) {
          updatePerm(state.formData.id, state.formData).then(() => {
            ElMessage.success('修改成功');
            cancel();
            handleQuery();
          });
        } else {
          state.formData.menuId = props.menuId;
          addPerm(state.formData).then(() => {
            ElMessage.success('新增成功');
            cancel();
            handleQuery();
          });
        }
      }
    });
  }

  /**
   * 重置表单
   */
  function resetForm() {
    dataFormRef.value.resetFields();

    state.urlPerm = {
      requestMethod: '',
      serviceName: '',
      requestPath: '',
    };
  }

  function cancel() {
    resetForm();
    state.dialog.visible = false;
  }

  function handleDelete(row: any) {
    const ids = [row.id || state.ids].join(',');
    ElMessageBox.confirm('确认删除已选中的数据项?', '警告', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    })
      .then(() => {
        deletePerms(ids).then(() => {
          ElMessage.success('删除成功');
          handleQuery();
        });
      })
      .catch(() => ElMessage.info('已取消删除'));
  }

  onMounted(() => {
    handleQuery();
  });
</script>

<style lang="scss" scoped>
  .component-container {
    margin-bottom: 20px;
  }
</style>
